package algorithm.Strings;

import java.util.*;

/**
 * @Author: 李浩然
 * @Date: 2021/1/5 10:34 上午
 */
public class LongestSubstring {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("arbacacfr"));
        System.out.println(lengthOfLongestSubstring("hkcpmprxxxqw"));
        System.out.println(lengthOfLongestSubstring("dvdf"));
        System.out.println(lengthOfLongestSubstring("tmmzuxt"));
        System.out.println(lengthOfLongestSubstring("jbpnbwwd"));
    }


    public static int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int right = 0, left = 0, res = 0;
        while (right < s.length()) {
            char c = s.charAt(right++);
            while (set.contains(c)) {
                set.remove(s.charAt(left++));
            }
            set.add(c);
            res = Math.max(res, right - left);
        }
        return res;
    }
}
